home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / C / CSOURCE.ZIP / DISSOLVE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-07  |  6.5 KB  |  228 lines

  1. /****************************************************************
  2. * FILE:    dissolve.c
  3. * DESC:    This file accepts two PCX files and does a dissolve
  4. *        between them.
  5. * HISTORY:    Created     7/03/1994
  6. * LAST CHANGED: 7/03/1994
  7. *    Copyright (c) 1994 by Scott Anderson
  8. *
  9. ****************************************************************/
  10.  
  11. /* ----------------------INCLUDES----------------------------- */
  12.  
  13. #include <conio.h>
  14. #include <stdio.h>
  15. #include <io.h>
  16. #include <math.h>
  17. #include <graph.h>
  18. #include <malloc.h>
  19. #include <memory.h>
  20. #include <string.h>
  21.  
  22. #include "define.h"
  23.  
  24. /* ----------------------DEFINES------------------------------ */
  25.  
  26. #define DISSOLVE_TWEENS    1
  27.  
  28. /* ----------------------PROTOTYPES--------------------------- */
  29.  
  30. int    getColor(PICTURE *pic, int x, int y, COLOR *color);
  31. int        tweenDissolve(PICTURE *src, PICTURE *dst);
  32.  
  33. /* ----------------------EXTERNALS---------------------------- */
  34.  
  35. /**** color routines ****/
  36. extern int        closestColor(int r, int g, int b, PALETTE *palPtr);
  37. extern void        collapseColors(PALETTE *palPtr);
  38.  
  39. /**** other variables ****/ 
  40. /* ID of palette currently being displayed */
  41. extern int         CurrentPal;
  42.  
  43. extern char        *OutFilename;
  44.  
  45. /* set from last picture loaded */
  46. extern int        Xmin, Ymin, Xmax, Ymax;
  47.  
  48. /***** variables used to compute intermediate images ****/
  49. /* number of colors in tweened image before reduction*/
  50. extern int         Ncolors;
  51.  
  52. /* r, g, b frequency counter array */
  53. extern unsigned int far Freq[MAX_COMP][MAX_COMP][MAX_COMP];
  54.  
  55. /* tweened images red, grn, and blu components*/
  56. extern unsigned char far Red[MAX_WIDE][MAX_TALL];
  57. extern unsigned char far Grn[MAX_WIDE][MAX_TALL];
  58. extern unsigned char far Blu[MAX_WIDE][MAX_TALL];
  59.  
  60. extern PALETTE TweenPal;            /* resulting palette */
  61.  
  62. /* ----------------------GLOBAL DATA--------------------------- */
  63.  
  64. PICTURE *Src;        /* source & destination picture pointers */
  65. PICTURE *Dst;
  66.  
  67. int        Tweens;
  68.  
  69. /*****************************************************************
  70. * FUNC: main (int argc, char *argv[])
  71. * DESC: Read in a filename to load
  72. *****************************************************************/
  73.  
  74. main (int argc, char *argv[])
  75. {
  76.     int        segment;
  77.  
  78.     /* load the pcx file if one is given */
  79.     if ((3 > argc) || (argc > 5)) {
  80.         printf("Usage: dissolve <source> <dest> [<steps> [<output>]]\n\n");
  81.         printf("Where: <source> is the source PCX filename\n");
  82.         printf("       <dest>   is the destination filename\n");
  83.         printf("       <steps>  is the optional sequence size\n");
  84.         printf("                (the max is %d, the default is %d)\n",
  85.                                 MAX_TWEENS, DISSOLVE_TWEENS+2);
  86.         printf("       <output> is the optional output filename\n");
  87.         printf("                (defaults to no output)\n");
  88.         printf("Note:  The output filename can be at most %d characters long.\n",
  89.                                         MAX_NAME_SIZE);
  90.         printf("       The PCX extension is added automatically, so don't\n");
  91.         printf("       include it in the filename.\n");
  92.         printf("       Morph only accepts PCX files with %d X %d resolution\n",
  93.                                         MAX_WIDE, MAX_TALL);
  94.         printf("       and %d colors.\n", COLORS);
  95.         exit(0);
  96.     }
  97.     if (argc > 3) {
  98.         /* subtract two from the sequence count (for the source
  99.             and target) to get the tween count */
  100.         Tweens = clip (atoi(argv[3]) - 2, 1, MAX_TWEENS);
  101.         if (argc > 4)
  102.             OutFilename = argv[4];
  103.     }
  104.     else
  105.         Tweens = DISSOLVE_TWEENS;
  106.     printf("Loading the file %s\n", argv[1]);
  107.     Src = loadPicture(argv[1]);
  108.     if (Src == NULL)
  109.         quit(MEMORY_ERR, "");
  110.     printf("Loading the file %s\n", argv[2]);
  111.     Dst = loadPicture(argv[2]);
  112.     if (Dst == NULL)
  113.         quit(MEMORY_ERR, "");
  114.  
  115.     setGraphicsMode();
  116.     tweenDissolve(Src, Dst);
  117.     setTextMode();
  118. }
  119.  
  120. /*****************************************************************
  121. * FUNC: int    tweenDissolve(PICTURE *src, PICTURE *dst)
  122. * DESC: calculate a pixel to plot, from the warping function
  123. *****************************************************************/
  124.  
  125. #define TOTAL_WEIGHT        (100)    /* Good for up to 99 tweens */
  126.  
  127. tweenDissolve(PICTURE *src, PICTURE *dst)
  128. {
  129.     int color;
  130.     int    x,y;
  131.     COLOR scolor, dcolor;
  132.     int t, i, p;
  133.     int r, g, b;
  134.     unsigned int srcweight, srcpaletteindex;
  135.     unsigned int dstweight, dstpaletteindex;
  136.  
  137.     displayPicture(src);
  138.     saveScreen(&src->pal);
  139.  
  140.     /* src is on screen, now tween to the target */
  141.     for (t = 1; t <= Tweens; t++) {
  142.         dstweight = t * TOTAL_WEIGHT / (Tweens+1);
  143.         srcweight = TOTAL_WEIGHT - dstweight;
  144.  
  145.         /* Zero out the buffers */
  146.         initFreq();
  147.         _fmemset(Red, 0, sizeof Red);
  148.         _fmemset(Grn, 0, sizeof Grn);
  149.         _fmemset(Blu, 0, sizeof Blu);
  150.  
  151.         /* Go through the screen positions */
  152.         for (y = Ymin; y <= Ymax; y++)    {
  153.             if (quitCheck())
  154.                 quit(0, "");
  155.             for (x = Xmin; x <= Xmax; x++)    {    
  156.                 getColor(src, x, y, &scolor);
  157.                 getColor(dst, x, y, &dcolor);
  158.                 r = (scolor.r * srcweight +    dcolor.r * dstweight)
  159.                                 / TOTAL_WEIGHT;
  160.                 g = (scolor.g * srcweight +    dcolor.g * dstweight)
  161.                                 / TOTAL_WEIGHT;
  162.                 b = (scolor.b * srcweight +    dcolor.b * dstweight)
  163.                                 / TOTAL_WEIGHT;
  164.                 if (Freq[r][g][b] == 0)        /* A new color */
  165.                     Ncolors++;
  166.                 /* Keep it to one byte */
  167.                 if (Freq[r][g][b] < MAX_FREQ)
  168.                     Freq[r][g][b]++;
  169.                 /* put the RGB components of each pixel into a
  170.                                         temporary screen buffer */
  171.                 Red[x][y] = r;
  172.                 Grn[x][y] = g;
  173.                 Blu[x][y] = b;
  174.             }
  175.         }
  176.         collapseColors(&TweenPal);
  177.         setPalette(&TweenPal);
  178.  
  179.         for (y = Ymin; y <= Ymax; y++)    {
  180.             if (quitCheck())
  181.                 quit(0, "");
  182.             for (x = Xmin; x <= Xmax; x++)    {
  183.                 color = closestColor(    Red[x][y],
  184.                                         Grn[x][y],
  185.                                         Blu[x][y],
  186.                                         &TweenPal);
  187.                 _setcolor (color);
  188.                 _setpixel (x, y);
  189.             }
  190.         }
  191.         if (!OutFilename) {    /* no output file name was given */
  192.             beep();
  193.             beep();
  194.             waitForKey();    /* so pause to enjoy the pictures */
  195.         }
  196.         else
  197.             saveScreen(&TweenPal);
  198.     }
  199.     CurrentPal = 0;         /* force a new palette */
  200.     displayPicture(dst);
  201.     saveScreen(&dst->pal);
  202. }
  203.  
  204. /*****************************************************************
  205. * FUNC: int    getColor(PICTURE *pic, int x, int y, COLOR *color)
  206. * DESC: Return the index and the RGB color at the given
  207. *        x,y coordinate.
  208. *****************************************************************/
  209.  
  210. int
  211. getColor(PICTURE *picture, int x, int y, COLOR *color)
  212. {
  213.     int        paletteIndex;
  214.  
  215.     paletteIndex = PIXEL (picture, x, y);
  216.     color->r = picture->pal.c[paletteIndex].r;
  217.     color->g = picture->pal.c[paletteIndex].g;
  218.     color->b = picture->pal.c[paletteIndex].b;
  219.     return (paletteIndex);
  220. }
  221.  
  222.  
  223.